package com.lge.pitchingcoins.ui.clients.layer;

import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.interval.CCIntervalAction;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.actions.interval.CCTintBy;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCMultiplexLayer;
import org.cocos2d.menus.CCMenu;
import org.cocos2d.menus.CCMenuItemFont;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.types.ccColor3B;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.lge.pitchingcoins.R;
import com.lge.pitchingcoins.ui.clients.ClientsMainActivityWithCocos;
import com.lge.pitchingcoins.util.Util;

public class ClientsMainLayer extends CCLayer {
    public static final int NICKNAME_EDITTEXT_FOCUS_CLEAR = 1000;
    public static final int MAKE_TOAST_FOR_NICKNAME = 1001;
    public static final int MAKE_TOAST_FOR_WIFI_CONNECTION = 1002;
    public static final int NICKNAME_EDITTEXT_VISIBILITY_GONE = 1003;
    public static final int NICKNAME_EDITTEXT_VISIBILITY_VISIBLE = 1004;


    private Context mContext = null;

    private EditText nickNameEditText = null;

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == NICKNAME_EDITTEXT_FOCUS_CLEAR) {
                nickNameEditText.clearFocus();
                
                InputMethodManager inputMethodManager = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(nickNameEditText.getWindowToken(), 0);
            } else if (msg.what == MAKE_TOAST_FOR_NICKNAME) {
                Toast.makeText(mContext, mContext.getString(R.string.inputNicknameText), Toast.LENGTH_SHORT).show();
            } else if (msg.what == MAKE_TOAST_FOR_WIFI_CONNECTION) {
                Toast.makeText(mContext, mContext.getString(R.string.requireWifiConnectionText), Toast.LENGTH_SHORT).show();
            } else if (msg.what == NICKNAME_EDITTEXT_VISIBILITY_GONE) {
                nickNameEditText.setVisibility(View.GONE);
            } else if (msg.what == NICKNAME_EDITTEXT_VISIBILITY_VISIBLE) {
                nickNameEditText.setVisibility(View.VISIBLE);
            }
        }
    };


    // Constructors
    public ClientsMainLayer(Context context) {
        super();

        mContext = context;

        initComponent();
    }


    // Callback Methods
    @Override
    public boolean ccTouchesEnded(MotionEvent event) {
        mHandler.sendEmptyMessage(NICKNAME_EDITTEXT_FOCUS_CLEAR);

        return true;
    }

    @Override
    public void onEnter() {
        mHandler.sendEmptyMessage(NICKNAME_EDITTEXT_VISIBILITY_VISIBLE);

        super.onEnter();
    }

    @Override
    public void onExit() {
        mHandler.sendEmptyMessage(NICKNAME_EDITTEXT_VISIBILITY_GONE);

        super.onExit();
    }


    // Member methods
    public void initComponent() {
        setIsTouchEnabled(true);

        nickNameEditText = new EditText(mContext);
        try {
            nickNameEditText.setText(Util.getDeviceName(mContext.getContentResolver()).replaceAll("\\p{Space}", ""));
        } catch (Exception e) {
            e.printStackTrace();
            
            nickNameEditText.setText("PitchingCoins");
        }
        nickNameEditText.setTextColor(mContext.getResources().getColor(R.color.white));
        nickNameEditText.setLines(1);

        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams((int)getContentSize().width * 2 / 3, 100);
        CCDirector.sharedDirector().getActivity().addContentView(nickNameEditText, layoutParams);


        CCMenuItemFont.setFontSize(100);

        CCMenuItemFont gameConnectButton = CCMenuItemFont.item(mContext.getString(R.string.gameConnectButtonText), this, "gameConnectButtonPressed");

        CCIntervalAction colorAction = CCTintBy.action(0.5f, ccColor3B.ccc3(0, -255, -255));
        CCIntervalAction colorBack = colorAction.reverse();
        CCIntervalAction sequence = CCSequence.actions(colorAction, colorBack);
        gameConnectButton.runAction(CCRepeatForever.action(sequence));

        CCMenu menu = CCMenu.menu(gameConnectButton);
        menu.alignItemsVertically();

        addChild(menu);
    }

    public void gameConnectButtonPressed(Object sender) {
        ConnectivityManager connectivityManager = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfoForWifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

        if (networkInfoForWifi.isConnected()) {
            if (nickNameEditText.getText().toString().trim().length() > 0) {
                ((ClientsMainActivityWithCocos)mContext).setNickname(nickNameEditText.getText().toString().replaceAll("\\p{Space}", ""));

                ((CCMultiplexLayer)getParent()).setTag(1);
                ((CCMultiplexLayer)getParent()).switchTo(1);
            } else {
                mHandler.sendEmptyMessage(MAKE_TOAST_FOR_NICKNAME);
            }
        } else {
            mHandler.sendEmptyMessage(MAKE_TOAST_FOR_WIFI_CONNECTION);
        }
    }
}